package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.adapter.MySellKtAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.OrderListBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MyBalancePresenter;
import com.mz.ozy.mvp.view.MyBalanceContract;
import com.mz.ozy.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/2 0002.
 */
public class MyAccountActivity extends BaseActivity<MyBalanceContract.View, MyBalancePresenter> implements MyBalanceContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.ll_btn_dc)
    LinearLayout llBtnDc;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_integral)
    TextView tvIntegral;
    @BindView(R.id.tt_balance)
    TextView ttBalance;
    @BindView(R.id.tt_integral)
    TextView ttIntegral;

    //private int pageNo = 1;
    private int pageNo1 = 1;
    private int pageNo2 = 1;
    private int tag = 0;
    private List<OrderListBean.PageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private MySellKtAdapter mAdapter;
    private OrderListBean dataItem;
    private String balance;
    private String integral;
    private String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_account);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("我的欧花");
        url = Constants.TransCode.TransCode10012;
        balance = getIntent().getStringExtra("balance");
        tvBalance.setText(Utils.decimalUtils(balance, 2));
        tvBalance.setTextColor(0xff00743a);
        ttBalance.setTextColor(0xff00743a);
        integral = getIntent().getStringExtra("integral");
        tvIntegral.setText(integral);
        tvIntegral.setText(Utils.decimalUtils(integral, 2));
        initView();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MySellKtAdapter(mBeanList, 3);
        recyclerview.setAdapter(mAdapter);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                int pageNo = 0;
                if(url==Constants.TransCode.TransCode10012) {
                    pageNo1 = pageNo1 + 1;
                    pageNo = pageNo1;
                }else{
                    pageNo2 = pageNo2 + 1;
                    pageNo = pageNo2;
                }
                mPresenter.getInfo(MyAccountActivity.this, pageNo + "", url);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                int pageNo = 1;
                if(url==Constants.TransCode.TransCode10012) {
                    pageNo1 = 1;
                }else{
                    pageNo2 = 1;
                }
                mPresenter.getInfo(MyAccountActivity.this, pageNo + "", url);
            }
        });
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        pageNo = 1;
//        mPresenter.getInfo(MyAccountActivity.this, pageNo + "", url);
//    }

    @OnClick({R.id.tv_recharge, R.id.tv_exchange, R.id.tv_balance, R.id.tv_integral})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_recharge:
                MeRechargeActivity.go(this, dataItem.getUser_balance());
                break;
            case R.id.tv_exchange:
                if (dataItem != null) {
                    Intent intent = new Intent(MyAccountActivity.this, ExchangeObActivity.class);
                    intent.putExtra("balance", dataItem.getBonusProfit());
                    startActivity(intent);
                }
                break;
            case R.id.tv_balance:
                url = Constants.TransCode.TransCode10012;
                tag=0;
                mPresenter.getInfo(MyAccountActivity.this, 1 + "", url);
                llBtnDc.setVisibility(View.VISIBLE);
                tvBalance.setTextColor(0xff00743a);
                tvIntegral.setTextColor(0xff000000);
                ttBalance.setTextColor(0xff00743a);
                ttIntegral.setTextColor(0xff000000);
                tvCenterTitle.setText("我的欧花");
                break;
            case R.id.tv_integral:
                url = Constants.TransCode.TransCode10018;
                tag=0;
                mPresenter.getInfo(MyAccountActivity.this, 1 + "", url);
                llBtnDc.setVisibility(View.GONE);
                tvBalance.setTextColor(0xff000000);
                tvIntegral.setTextColor(0xff00743a);
                ttBalance.setTextColor(0xff000000);
                ttIntegral.setTextColor(0xff00743a);
                tvCenterTitle.setText("我的积分");
                break;
        }
    }

    @Override
    public void getBanclceInfo(OrderListBean bean) {
        dataItem = bean;
        if(url==Constants.TransCode.TransCode10012) {
            tvBalance.setText(Utils.decimalUtils(bean.getUser_balance(), 2));
            tvIntegral.setText(Utils.decimalUtils(integral, 2));
        }else{
            tvBalance.setText(Utils.decimalUtils(balance, 2));
            tvIntegral.setText(Utils.decimalUtils(bean.getUser_integral(), 2));
        }
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }
}
